﻿#pragma once
#include "HashTable.h"

namespace bit {
	template <class K, class V, class Hash = HashFunc<K>>
	class unordered_map {
		struct MapKeyOfT {
			const K& operator() (const pair<K, V>& kv) { return kv.first; }
		};
	public:
		typedef typename hash_bucket::HashTable<K, pair<const K, V>, MapKeyOfT, Hash>::iterator iterator;
		typedef typename hash_bucket::HashTable<K, pair<const K, V>, MapKeyOfT, Hash>::const_iterator const_iterator;
		iterator begin() { return _ht.begin(); }
		iterator end() { return _ht.end(); }
		const_iterator begin() const { return _ht.begin(); }
		const_iterator end() const { return _ht.end(); }
		pair<iterator, bool> insert(const pair<K, V>& kv) { return _ht.Insert(kv); }
		V& operator[](const K& key) {
			pair<iterator, bool> ret = _ht.Insert(make_pair(key, V()));
			return ret.first->second;
		}
		const V& operator[](const K& key) const {
			pair<iterator, bool> ret = _ht.Insert(make_pair(key, V()));
			return ret.first->second;
		}

		iterator find(const K& key) { return _ht.Find(key); }
		bool erase(const K& key) { return _ht.Erase(key); }

	private:
		hash_bucket::HashTable<K, pair<const K, V>, MapKeyOfT, Hash> _ht;
	};

	void test_map() {
		unordered_map<string, string> dict;
		dict.insert(make_pair("sort", "排序"));
		dict.insert(make_pair("string", "字符串"));
		dict.insert(make_pair("insert", "插入"));
		for (auto& kv : dict) {
			//kv.first += 'x';
			//kv.second += 'x';
			cout << kv.first << ":" << kv.second << endl;
		}
		cout << endl;

		string arr[] = { "苹果", "西瓜", "苹果", "西瓜", "苹果", "苹果", "西瓜", "苹果", "香蕉", "苹果", "香蕉" };
		unordered_map<string, int> countMap;
		for (auto& e : arr)
			countMap[e]++;

		for (auto& kv : countMap)
			cout << kv.first << ":" << kv.second << endl;
	}
}